package cn.learn.server.util;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.atomic.AtomicReference;

public class RequestPathMetricsCollector {

    private final Map<String, PathStatsQueue> immutableRequestsMap;

    public RequestPathMetricsCollector() {
        final Map<String, PathStatsQueue> requestsMap = new HashMap<>();
        requestsMap.put("create", new PathStatsQueue("create"));
        this.immutableRequestsMap = java.util.Collections.unmodifiableMap(requestsMap);
    }

    public void registerRequest(int type, String path) {
        PathStatsQueue pathStatsQueue = immutableRequestsMap.get("create");
        if (pathStatsQueue != null) {
            pathStatsQueue.registerRequest(path);
        }
    }

    class PathStatsQueue {
        private final String requestTypeName;
        private final boolean isWriteOperation;
        private final AtomicReference<ConcurrentLinkedQueue<String>> currentSlot;
        private final LinkedBlockingQueue<Map<String, Integer>> requestPathStats;

        public PathStatsQueue(String requestType) {
            this.requestTypeName = requestType;
            this.isWriteOperation = true;
            requestPathStats = new LinkedBlockingQueue<>(60);
            currentSlot = new AtomicReference<>(new ConcurrentLinkedQueue<>());
        }

        public void registerRequest(String path) {
            currentSlot.get().offer(path);
        }
    }
}
